// riderDistribution/pages/order/order.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        codeMessage: 0,
        styleClass: 1,
        number: 4,
        navItem: [ {
            name: "新任务",
            id: "5"
        }, {
            name: "待取货",
            id: "2"
        }, {
            name: "配送中",
            id: "4"
        }, {
            name: "已完成",
            id: "127"
        } ],
        page: 1,
        keyword: "",
        lat: "",
        lng: "",
        type: 5,
        inputValue: "",
        list: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getLocate();
        let index = 0;
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getList();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //调用定位
    getLocate() {
        let that = this;
        common.getAppBase().then(() => {
            common.getCurrentLocation(that.data.appBase.location_key).then(() => {
                that.getList();
            });
        });
    },
    chooseType: function(e) {
        this.setData({
            type: e.currentTarget.dataset.id,
            page: 1,
            list: []
        });
        this.getList();
    },
    getList: function() {
        let _this = this;
        let {type: type, page: page, keyword: keyword, lat: lat, lng: lng} = _this.data;
        let params = {
            type: type,
            page: page,
            keyword: keyword,
            lat: lat,
            lng: lng
        };
        common.request({
            endpoint: "Rider/rider/rider-order",
            method: "post",
            data: params,
            success: res => {
                let list = res.extra;
                list = page == 1 ? list : _this.data.list.concat(list);
                page = page + 1;
                _this.setData({
                    list: list,
                    page: page
                });
            }
        });
    },
    passwordNum: function(e) {
        this.setData({
            inputValue: e.detail
        });
    },
    relTap: function(e) {
        wx.reLaunch({
            url: e.currentTarget.dataset.url
        });
    },
    //确认送达-核销
    confirm_dev(e) {
        var code = e.currentTarget.dataset.code;
        if (code == 1) {
            this.setData({
                codeMessage: 1,
                chooseOrderId: e.currentTarget.dataset.id
            });
        } else {
            this.setData({
                chooseOrderId: e.currentTarget.dataset.id
            });
            this.checkCode();
        }
    },
    closeCode: function() {
        this.setData({
            codeMessage: 0,
            inputValue: ""
        });
    },
    checkCode: function() {
        let _this = this;
        common.request({
            endpoint: "Rider/rider/check-code",
            method: "post",
            data: {
                order_id: _this.data.chooseOrderId,
                code: _this.data.inputValue,
                lat: _this.data.lat,
                lng: _this.data.lng
            },
            success: res => {
                wx.showToast({
                    iocn: "none",
                    title: "确认取货",
                    duration: 2e3
                });
                _this.setData({
                    page: 1,
                    list: [],
                    codeMessage: 0
                });
                _this.getLocate();
            },
            fail(err) {
                wx.showToast({
                    iocn: "none",
                    title: err.message,
                    duration: 2e3
                });
            }
        });
    },
    //立即接单
    grabOrder: function(e) {
        console.log(e);
        let _this = this;
        common.request({
            endpoint: "Rider/rider/grap-order",
            method: "post",
            data: {
                order_id: e.currentTarget.dataset.id,
                lat: _this.data.lat,
                lng: _this.data.lng,
                delivery_repertory_id: e.currentTarget.dataset.repertory
            },
            success: res => {
                wx.showToast({
                    icon: "none",
                    title: "抢单成功",
                    duration: 2e3
                });
                _this.setData({
                    codeMessage: 0,
                    page: 1,
                    list: []
                });
                setTimeout(() => {
                    _this.getLocate();
                }, 2e3);
            },
            fail(err) {
                wx.showToast({
                    icon: "none",
                    title: err.message,
                    duration: 2e3
                });
            }
        });
    },
    //取消订单
    cancelOrder: function(e) {
        let _this = this;
        common.request({
            endpoint: "Rider/rider/cancel-grap-order",
            method: "post",
            data: {
                order_id: e.currentTarget.dataset.id,
                lat: _this.data.lat,
                lng: _this.data.lng
            },
            success: res => {
                wx.showToast({
                    icon: "none",
                    title: "取货订单成功",
                    duration: 2e3
                });
                _this.setData({
                    page: 1,
                    list: []
                });
                setTimeout(() => {
                    _this.getLocate();
                }, 2e3);
            }
        });
    },
    //确认取货
    pickUp: function(e) {
        let _this = this;
        common.request({
            endpoint: "Rider/rider/rider-pick",
            method: "post",
            data: {
                order_id: e.currentTarget.dataset.id,
                lat: _this.data.lat,
                lng: _this.data.lng
            },
            success: res => {
                wx.showToast({
                    icon: "none",
                    title: "取货成功",
                    duration: 2e3
                });
                _this.setData({
                    page: 1,
                    list: []
                });
                setTimeout(() => {
                    _this.getLocate();
                }, 2e3);
            },
            fail: err => {
                wx.showModal({
                    title: "提示",
                    content: err.message,
                    showCancel: false
                });
            }
        });
    },
    concatMerchant: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        });
    },
    refresh: function() {
        this.setData({
            page: 1,
            list: []
        });
        this.getLocate();
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    recordValue: function(e) {
        this.setData({
            keyword: e.detail.value,
            page: 1,
            list: []
        });
        this.getLocate();
    }
});